package edu.whcp.comp2301.controller.viewController;

import edu.whcp.comp2301.dao.CommentDao;
import edu.whcp.comp2301.dao.NewsDao;
import edu.whcp.comp2301.pojo.Comment;
import edu.whcp.comp2301.pojo.News;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/page")
public class PageNewsController {
    @Autowired
    private NewsDao newsDao;

    @Autowired
    private CommentDao commDao;

    @RequestMapping("/news")
    public ModelAndView news(@RequestParam String category) {
        List<News> newsList = newsDao.getNewsListPage(category);
        return new ModelAndView("frontend/news", "newsList", newsList);
    }

    @RequestMapping(value = "/get/news", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ModelAndView listById(@RequestParam String id) {
        List<News> news=newsDao.getNews(id);
        return new ModelAndView("frontend/newsRead","news",news);
    }

    @RequestMapping("/homepage")
    public ModelAndView homepage() {
        List<News> newsList=newsDao.getNewsList();
        return new ModelAndView("frontend/index","homeNews",newsList);
    }

    @RequestMapping(value = "/get/comm",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public List<Comment> getComm(@RequestParam String id){
        return commDao.getComm(id);
    }

    @RequestMapping(value = "/add/comm",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Integer addComm(@RequestBody Comment comment){
        return commDao.addComment(comment);
    }
}
